package me.firstapp.api.bbs;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.api.service.EmailService;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;

@Controller
@RequestMapping(value = "/email")
@Api(value = "/email", description = "邮件相关接口")
public class EmailApi extends BaseApi {

	@Autowired
	private EmailService emailService;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@RequestMapping(value = "sendEmailCode.do", method = RequestMethod.POST)
	@ApiOperation(value = "发送邮件验证码", notes = "发送邮件验证码")
	public void sendEmailCode(String email, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<String> resultJsonObject = new SingleObject<String>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			if (StrUtils.isNULL(email)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "email");
			}
			if (!StrUtils.isMail(email)) {
				throw new ServiceException(StatusHouse.EMAIL_FORMAT_WRONG);
			}
			String _code = stringRedisTemplate.opsForValue().get(email);
			if (StrUtils.isNotNULL(_code)) {
				throw new ServiceException(StatusHouse.CODE_IS_EXIST);
			}

			String code = StrUtils.getVerifyCode(6);
			String content = "<p>你的验证码为：<strong>" + code + "</strong>，两分钟之内有效。【FirstApp.Me】</p>";
			emailService.sendEmail(email, content, "邮件验证码", apiClientId);
			stringRedisTemplate.opsForValue().set(email, code);
			stringRedisTemplate.expire(email, 2, TimeUnit.MINUTES);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "sendEmail.do", method = RequestMethod.POST)
	@ApiOperation(value = "发送邮件", notes = "发送邮件")
	public void sendEmail(@RequestBody Map<String, Object> body, String email, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<String> resultJsonObject = new SingleObject<String>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String title = (String) body.get("title");
			String content = (String) body.get("content");
			if (StrUtils.isNULL(email)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "email");
			}
			if (!StrUtils.isMail(email)) {
				throw new ServiceException(StatusHouse.EMAIL_FORMAT_WRONG);
			}

			emailService.sendEmail(email, content, title, apiClientId);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}
}
